<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <script>
      // 珠宝商店构造函数
      function JewelryStore(name,address,productlist=[]){
        this.name = name
        this.address = address
        this.productlist = productlist
      }
      // 添加商品到商店
      JewelryStore.prototype.addproduct = function(product){
        if(!product.name || !product.price){
          throw new Error('商品信息不完整')
        }
        this.productlist.push(product)
      }
      // 删除商品
      JewelryStore.prototype.removeProduct = function (product){
        const index = this.productlist.indexOf(product);
        if (index === -1) {
          throw new Error("未找到该商品！");
        }
        this.productlist.splice(index, 1);
        console.log(this);
      }
      // 计算商品总价
      JewelryStore.prototype.calculateTotalSales = function (){
        const  res = this.productlist.reduce((prev,item)=>{
          return prev+=item.price
        },0)
        return `总价为：${res}`
      }
      
      // 商店里的所有商品
      JewelryStore.prototype.listProducts = function (){
       const res = this.productlist.map((item)=>{return item.name})
       console.log(`商品有 ${res}`);
      }

      // 产品 构造函数
     function Product (name,price){
      this.name = name
      this.price = price
     }

    //  产品实例
     const ysz = new Product('玉手镯',29999)
     const zyez = new Product('足银耳坠',589)
     const sl = new Product('手链',399)

     const zbj = new JewelryStore('珠宝','杭州市',[ysz,zyez])
    
     zbj.addproduct(sl)
     zbj.removeProduct(sl)
     console.log(zbj.calculateTotalSales());
     zbj.listProducts()
    </script>
  </body>
</html>
